package com.un.ebs.sys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.sys.domain.UserGroup;
import com.un.ebs.sys.domain.UserGroupItemExt;
import com.un.ebs.sys.service.UserGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (UserGroup)表控制层
 *
 * @author liubo
 * @since 2020-08-23 11:52:17
 */
@RestController
@RequestMapping("userGroup")
@Api(value = "user-group")
public class UserGroupController {
    /**
     * 服务对象
     */
    @Resource
    private UserGroupService userGroupService;

    @PostMapping("update")
    @ApiOperation(value = "update", notes = "修改")
    public WebResult update(@RequestBody UserGroup req) {
        userGroupService.update(req.getId(), req.getName(), req.getRemark());
        return WebUtils.success();
    }

    @PostMapping("addUserGroupItem")
    @ApiOperation(value = "addUserGroupItem", notes = "用户组别添加用户")
    public WebResult update(@RequestParam String groupId, @RequestParam String userId) {
        userGroupService.addUser(groupId, userId);
        return WebUtils.success();
    }

    @GetMapping("list")
    @ApiOperation(value = "list", notes = "分页查询，用户组别")
    public WebResult<IPage<UserGroup>> listCustomer(@RequestParam(required = false) String searchText, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(userGroupService.query(searchText, pageSize, pageIndex));
    }

    @PostMapping("delete/{id}")
    @ApiOperation(value = "delete", notes = "根据主键删除单个 UserGroup")
    public WebResult delete(@PathVariable("id") String id) {
        userGroupService.delete(id);
        return WebUtils.success();
    }

    @PostMapping("deleteUser/{id}")
    @ApiOperation(value = "deleteUser", notes = "根据主键删除单个 UserGroupItem")
    public WebResult deleteUser(@PathVariable("id") String id) {
        userGroupService.deleteUser(id);
        return WebUtils.success();
    }

    @GetMapping("listGroupItem")
    @ApiOperation(value = "listGroupItem", notes = "查询，用户组别人员")
    public WebResult<IPage<UserGroupItemExt>> listUser(@RequestParam(required = false) String searchText, @RequestParam String groupId, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(userGroupService.queryItem(searchText, groupId, pageSize, pageIndex));
    }

    @GetMapping("listUserGroup")
    @ApiOperation(value = "listUserGroup", notes = "根据用户主键查询，用户组别")
    public WebResult<List<UserGroup>> listGroup(@RequestParam String userId) {
        return WebUtils.success(userGroupService.queryByUserId(userId));
    }

}
